home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / DialogLib / DialogModalLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-12  |  3.9 KB  |  165 lines  |  [TEXT/KAHL]

  1. /* 
  2.     Functions for handling modal dialogs.
  3.     
  4.     93/03/05 AIH
  5.     - Dialog is set to modal window layer
  6.     
  7.     92/03/06 AIH
  8.     - Added functions for setting the titles of the ok and cancel buttons
  9.     
  10.     92/03/02 AIH
  11.     - Changed extra window data into a pointer which is saved in
  12.     the window's refCon field. This simplified a lot of the code.
  13.     Thank goodness for data hiding! It made modifying this
  14.     library a breeze.
  15.  
  16.     92/02/20 AIH
  17.     - Simplified event handling
  18. */
  19.  
  20. #include "DialogLib.h"
  21. #include "DialogModalLib.h"
  22. #include "EventLib.h"
  23. #include "KeyLib.h"
  24. #include "ResourceLib.h"
  25. #include "WindowLib.h"
  26.  
  27. static struct {
  28.     short ok_id;            /* 'STR#' resource containing title of ok button */
  29.     short ok_index;        /* index to title of ok button */
  30.     short cancel_id;        /* 'STR#' resource containing title of cancel button */
  31.     short cancel_index;    /* index to title of ok cancel */
  32. } gModal;
  33.  
  34. /* initialize the modal dialog */
  35. void DlgModalInit(DialogPtr dlg)
  36. {
  37.     CStr255 str;
  38.     
  39.     if (gModal.ok_id) {
  40.         ResStr(gModal.ok_id, gModal.ok_index, str);
  41.         if (DlgItemValid(dlg, ok) && DlgTypeCtl(DlgType(dlg, ok)))
  42.             DlgCtlTitleSet(dlg, ok, str);
  43.     }
  44.     if (gModal.cancel_id) {
  45.         ResStr(gModal.cancel_id, gModal.cancel_index, str);
  46.         if (DlgItemValid(dlg, cancel) && DlgTypeCtl(DlgType(dlg, cancel)))
  47.             DlgCtlTitleSet(dlg, cancel, str);
  48.     }
  49. }
  50.  
  51. /* update the modal dialog */
  52. void DlgModalUpdate(DialogPtr dlg)
  53. {
  54.     DlgDefaultFrame(dlg, WinHasFocus(dlg) ? black : gray);
  55. }
  56.  
  57. /* activate the modal dialog */
  58. void DlgModalActivate(DialogPtr dlg, Boolean active)
  59. {
  60.     DlgDefaultFrame(dlg, active ? black : gray);
  61. }
  62.  
  63. /* interpret return, enter, command-period, and escape keys */
  64. void DlgModalKeyDown(DialogPtr dlg, EventRecord *event)
  65. {
  66.     char    key = 0;
  67.     short    item = 0;
  68.  
  69.     require(DlgValid(dlg));    
  70.  
  71.     /* handle return, enter, and cancel keys */
  72.     key = event->message;
  73.     if (key == returnKey || key == enterKey)
  74.         item = DlgDefault(dlg);
  75.     else if (KeyCancel(event))
  76.         item = cancel;
  77.  
  78.     /* simulate a click in an item (if it's an enabled control) */
  79.     if (item && DlgItemValid(dlg, item)) {
  80.         if (DlgTypeCtl(DlgType(dlg, item)) && DlgCtlEnabled(dlg, item)) {
  81.             DlgFlashButton(dlg, item);
  82.             DlgClick(dlg, item);
  83.         }
  84.         event->what = nullEvent;
  85.     }
  86. }
  87.  
  88. /* always return 0 if a filter function is provided for the dialog for
  89.     compatability with the standard ModalDialog */
  90. TicksType DlgModalAdjustSleep(DialogPtr dlg)
  91. {
  92.     return(DlgModalFilter(dlg) ? 0 : LONG_MAX);
  93. }
  94.  
  95. /* call the dialog's filter function */
  96. void DlgModalFilterPre(DialogPtr dlg, EventRecord *event)
  97. {
  98.     WindowPtr window = NULL;
  99.     Boolean stop = false;
  100.     short item = 0;
  101.     short part = 0;
  102.     
  103.     require(DlgValid(dlg));    
  104.     if (DlgModalFilter(dlg))
  105.         stop = DlgModalFilter(dlg)(dlg, event, &item, WinExtraPtr(dlg)->filterData);
  106.     if (stop)
  107.         DlgClick(dlg, item);
  108.     else if (event->what == mouseDown) {
  109.         /* beep if clicked outside of permitted locations */
  110.         stop = true;
  111.         switch (FindWindow(event->where, &window)) {
  112.         case inMenuBar:
  113.             stop = false;
  114.             break;
  115.         case inContent:
  116.         case inDrag:
  117.             stop = (window != (WindowPtr) dlg);
  118.             break;
  119.         }
  120.         if (stop) {
  121.             SysBeep(5);
  122.             event->what = nullEvent;
  123.         }
  124.     }
  125.     else if (event->what == keyDown || event->what == autoKey)
  126.         DlgModalKeyDown(dlg, event);
  127. }
  128.  
  129. /* return the filter procedure for the dialog */
  130. DlgModalFilterType DlgModalFilter(DialogPtr dlg)
  131. {
  132.     return(WinExtraPtr(dlg)->filter);
  133. }
  134.  
  135. /* set the filter procedure for the dialog */
  136. void DlgModalFilterSet(DialogPtr dlg, DlgModalFilterType filter, void *data)
  137. {
  138.     WinExtraPtr(dlg)->filter = filter;
  139.     WinExtraPtr(dlg)->filterData = data;
  140. }
  141.  
  142. /* set the title of the ok button */
  143. void DlgModalOk(short id, short index)
  144. {
  145.     gModal.ok_id = id;
  146.     gModal.ok_index = index;
  147. }
  148.  
  149. /* set the title of the cancel button */
  150. void DlgModalCancel(short id, short index)
  151. {
  152.     gModal.cancel_id = id;
  153.     gModal.cancel_index = index;
  154. }
  155.  
  156. /* run a modal dialog */
  157. short DlgModalRun(DialogPtr dlg)
  158. {
  159.     require(DlgValid(dlg));
  160.     DlgClick(dlg, 0);
  161.     while (! DlgClicked(dlg))
  162.         EventOne();
  163.     return(DlgClicked(dlg));
  164. }
  165.